package com.shty.wd.utils.module.related;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONStringer;

// This class denotes the result data of one search.
public class ResultData {
	
	private String projectId;
	private String webSite ;
	private List<Entity> friendsList;	
	
	public String getWebSite() {
		return webSite;
	}
	public void setWebSite(String webSite) {
		this.webSite = webSite;
	}
	
	public String getProjectId() {
		return projectId;
	}
	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}
	
	public List<Entity> getFriendsList() {
		return friendsList;
	}
	public void setFriendsList(List<Entity> friendsList) {
		this.friendsList = friendsList;
	}
	

	public ResultData() {
	}

	public ResultData(String projectId,String webSite, 	List<Entity> friendsList) {		
		this.webSite = webSite;
		this.projectId = projectId;	
		this.friendsList = friendsList;
	}
	
	// Generate the string outputs of this result data.
	public String creatJSON(){
		JSONStringer js = new JSONStringer() ;
		JSONArray searchReasultJSONObj = new JSONArray() ;
		for (Entity ent : friendsList) {
			JSONObject entityJSON = new JSONObject();
			Map<String,String> result = ent.getResult();
			for (Entry<String,String> entry : result.entrySet()) {
				if(entry.getKey().equals("result")==false)
					entityJSON.put(entry.getKey(), entry.getValue());
			}
			entityJSON.put("hasRelations", ((Entity) ent).getHasRelations()) ;
			entityJSON.put("basis", ent.getBasis());
			searchReasultJSONObj.add(entityJSON);
		}			
		js.object().key("projectId").value(projectId).key("webSite").value(webSite).key("friendsList").value(searchReasultJSONObj)
			.endObject();
		
	
		return js.toString();
	}


	@Override
	public String toString() {
		return "ResultData [projectId="+ projectId+ ",webSite=" + webSite + ", friendsList="
				+ friendsList + "]";
	}

}
